<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <img src="" id="qrcode" alt="">
    <div id="status-div"></div>
    <script>
        const status = {
            0: "未授权",
            1: "已授权",
            2: "超时"
        }
        const qrcode = document.getElementById("qrcode")
        const statusDiv = document.getElementById("status-div")
        let userId = null;
        statusDiv.innerText = status[0]
        fetch("/qrcode").then(res => res.json()).then(res => {
            console.log(res);
            userId = res.userId;
            qrcode.src = res.code;
            const timer = setInterval(() => {
                // 轮询检查状态
                fetch(`/check/${userId}`).then(res => res.json()).then(res => {
                    statusDiv.innerText = status[res.status]
                    console.log(res);
                    // 如果状态是超时或者已授权，停止轮询
                    if (res.status !== 0) {
                        clearInterval(timer)
                    }
                })
            }, 1000);
        });
    </script>
</body>

</html>